package vnetlab.ui.editor;

import com.mxgraph.model.mxCell;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.TransferHandler;

import vnetlab.ui.editor.EditorActions.HistoryAction;
import com.mxgraph.swing.util.mxGraphActions;
import com.mxgraph.util.mxResources;

import javax.swing.JOptionPane;
import vnetlab.elements.Element;
import vnetlab.manager.Manager;
import vnetlab.manager.Status;
import vnetlab.ui.editor.EditorActions.EditAction;
import vnetlab.ui.editor.EditorActions.HideTerminalAction;
import vnetlab.ui.editor.EditorActions.ShowTerminalAction;

public class EditorPopupMenu extends JPopupMenu
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3132749140550242191L;

	public EditorPopupMenu(BasicGraphEditor editor)
	{

                mxCell selectedCell = (mxCell) editor.getGraphComponent().getGraph().getSelectionCell();
		boolean selected = selectedCell != null; //!editor.getGraphComponent().getGraph().isSelectionEmpty();

                boolean isRunning = false;
                boolean isVisible = false;
                boolean isSwitch = false;
                
                if(selected){
                     Element element = Manager.getInstance().getElement(selectedCell.getId());
                     isRunning = element.getStatus() == Status.EXECUTE ;
                     isVisible = element.isTerminalOpened();

                     isSwitch = element.getType().equalsIgnoreCase("Switch");
                }
//                mxCell c= (mxCell)editor.getGraphComponent().getGraph().getSelectionCell();
//                JOptionPane.showMessageDialog(null, c.getValue()+" "+c.getId());
                
//		add(editor.bind(mxResources.get("undo"), new HistoryAction(true),
//				"/vnetlab/ui/images/undo.gif"));
//		addSeparator();
//
//		add( editor.bind(mxResources.get("cut"), TransferHandler
//						.getCutAction(),
//						"/vnetlab/ui/images/cut.gif"))
//				.setEnabled(selected);
//		add( editor.bind(mxResources.get("copy"), TransferHandler
//						.getCopyAction(),
//						"/vnetlab/ui/images/copy.gif"))
//				.setEnabled(selected);
//		add(editor.bind(mxResources.get("paste"), TransferHandler
//				.getPasteAction(),
//				"/vnetlab/ui/images/paste.gif"));
//
//		addSeparator();

		add(editor.bind(mxResources.get("delete"), mxGraphActions.getDeleteAction(),"/vnetlab/ui/images/delete.gif")).setEnabled(selected);

//		addSeparator();
//
//		// Creates the format menu
//		JMenu menu = (JMenu) add(new JMenu(mxResources.get("format")));
//
//		EditorMenuBar.populateFormatMenu(menu, editor);
//
//		// Creates the shape menu
//		menu = (JMenu) add(new JMenu(mxResources.get("shape")));
//
//		EditorMenuBar.populateShapeMenu(menu, editor);

		addSeparator();

//		add(editor.bind(mxResources.get("edit"), mxGraphActions
//						.getEditAction())).setEnabled(selected);

                add(editor.bind(mxResources.get("edit"), new EditAction())).setEnabled(selected);

		

//		add(editor.bind(mxResources.get("selectVertices"), mxGraphActions
//				.getSelectVerticesAction()));
//		add(editor.bind(mxResources.get("selectEdges"), mxGraphActions
//				.getSelectEdgesAction()));
//
//		addSeparator();
//
//		add(editor.bind(mxResources.get("selectAll"), mxGraphActions
//				.getSelectAllAction()));

                if(!isSwitch && Manager.getInstance().getStatus() == Status.EXECUTE){
                    addSeparator();
                    add(editor.bind("Pause", new EditorActions.PauseAction(), "/vnetlab/ui/images/pause.png")).setEnabled(isRunning);
                    add(editor.bind("Resume", new EditorActions.ResumeAction(),"/vnetlab/ui/images/resume.png")).setEnabled(!isRunning);
                    add(editor.bind("Show Terminal", new ShowTerminalAction(),"/vnetlab/ui/images/show.png")).setEnabled(!isVisible);
                    add(editor.bind("Hide Terminal", new HideTerminalAction(),"/vnetlab/ui/images/hide.png")).setEnabled(isVisible);
                }
	}

}
